#include "qmyETM.h"
#include "qizvodvnes.h"
#include "qizvodkorekcija.h"


QMyETM::QMyETM(QWidget *parent)
: QMyBaseWidget(parent)
,ETMVnes(0)
,ETMKorekcija(0)
,ETMLista(0)
,m_row(0)
{
	ui.setupUi(this);
	pressF4();
}

QMyETM::~QMyETM()
{

}

void QMyETM::refresh()
{
}
void QMyETM::pressF2()
{
	closeAllForm();
	ETMVnes = showMyWidget<QETMVnes>(ETMVnes); 
    ETMVnes->setCategoryWidget(this);
	connect(ETMVnes,SIGNAL(closeW()),this,SLOT(closeW()));
	connect(ETMVnes,SIGNAL(scallKomintenti(QWidget*, QString)),this,SLOT(callKomintenti(QWidget*, QString)));
	connect(ETMVnes,SIGNAL(scallArtikli(QWidget*, QString)),this,SLOT(callArtikli(QWidget*, QString)));
	connect(ETMVnes,SIGNAL(scallIspratnici(QWidget*, QString)),this,SLOT(callIspratnici(QWidget*, QString)));
	connect(ETMVnes,SIGNAL(scallProETM(QWidget*, QString)),this,SLOT(callProETM(QWidget*, QString)));

	emit disablePrint(true);
}
void QMyETM::pressF3()
{
    if(!ETMLista)
    {
        return;
    }
    int id = ETMLista->getSelectedId();
    closeAllForm();
    ETMKorekcija = showMyWidget<QETMKorekcija>(ETMKorekcija); 
    ETMKorekcija->setCategoryWidget(this);
    connect(ETMKorekcija,SIGNAL(closeW()),this,SLOT(closeW()));
    connect(ETMKorekcija,SIGNAL(scallKomintenti(QWidget*, QString)),this,SLOT(callKomintenti(QWidget*, QString)));
    connect(ETMKorekcija,SIGNAL(scallArtikli(QWidget*, QString)),this,SLOT(callArtikli(QWidget*, QString)));
	connect(ETMKorekcija,SIGNAL(scallIspratnici(QWidget*, QString)),this,SLOT(callIspratnici(QWidget*, QString)));
	connect(ETMKorekcija,SIGNAL(scallProETM(QWidget*, QString)),this,SLOT(callProETM(QWidget*, QString)));
    ETMKorekcija->init(id);
	emit disablePrint(true);
}
void QMyETM::pressF4()
{
    closeAllForm();
    ETMLista = showMyWidget<QETMLista>(ETMLista); 
    ETMLista->setCategoryWidget(this);
	ETMLista->setRow(m_row);
    connect(ETMLista, SIGNAL(closeW()), this, SLOT(closeWLista()));
    connect(ETMLista, SIGNAL(procF2()), this, SLOT(procF2()));
    connect(ETMLista, SIGNAL(procF3()), this, SLOT(procF3()));
    connect(ETMLista, SIGNAL(procDel()), this, SLOT(procDel()));
	connect(ETMLista, SIGNAL(sETMToIzvod(QString&, QString&, QString&, QString&)), this, SLOT(retETMToIzvod(QString&, QString&, QString&, QString&)));
	emit disablePrint(false);

}
void QMyETM::procF2()
{
    pressF2();
}
void QMyETM::procF3()
{
    pressF3();
}
void QMyETM::procDel()
{

}
void QMyETM::pressEscape()
{
    this->raise();
    this->setFocus();
	emit disablePrint(true);

    emit signCloseMyWidget();
}
void QMyETM::closeW()
{
    pressF4();
}
void QMyETM::closeWLista()
{
    pressEscape();
}

void QMyETM::closeAllForm()
{
    ETMKorekcija = deleteMyWidget<QETMKorekcija>(ETMKorekcija);
    ETMVnes = deleteMyWidget<QETMVnes>(ETMVnes);
	if (ETMLista)
	{
		m_row = ETMLista->getRow();
	}
    ETMLista = deleteMyWidget<QETMLista>(ETMLista);
}
void QMyETM::callKomintenti(QWidget* myobj, QString text)
{
    emit signKomintentiFromIspratnica(myobj, text);
}

void QMyETM::callArtikli(QWidget* myobj, QString text)
{
    emit signArtikliFromIspratnica(myobj, text);
}

void QMyETM::callIspratnici(QWidget* myobj, QString text)
{
	emit signImportArtikliFromIspratnici(myobj, text);
}

void QMyETM::callProETM(QWidget* myobj, QString text)
{
	emit signImportArtikliFromProETM(myobj, text);
}

void QMyETM::sendPrintMessage()
{
    emit signPrint();
}

QStandardItemModel* QMyETM::getModel()
{
    QStandardItemModel * modelExt;
    if (ETMLista)
    {
        modelExt = ETMLista->model;
    } 
    else
    {
        modelExt = NULL;
    }
    return modelExt;
}

QStandardItemModel* QMyETM::getModel2()
{
    QStandardItemModel * modelExt;
    if (ETMLista)
    {
        modelExt = ETMLista->model2;
    } 
    else
    {
        modelExt = NULL;
    }
    return modelExt;
}

QStringList QMyETM::getIspratnicaInfo()
{
    QStringList modelExt;
    modelExt.clear();

    if (ETMLista)
    {
        modelExt = ETMLista->geInfo();
    } 
    else
    {
        modelExt;
    }
    return modelExt;
}

void QMyETM::retETMToIzvod(QString& text6, QString& text8, QString& text9, QString& textName)
{
	QWidget *sour = getSourceWidget();
	if (qobject_cast<QIzvodVnes*>(sour))
	{
		QIzvodVnes *dest = qobject_cast<QIzvodVnes*>(sour);
//		dest->setETM(text6, text8, text9, textName);
		emit eupdateNanigator(dest->getCategoryWidget(), this);
		dest->setFocusLine2();
	}
	if (qobject_cast<QIzvodKorekcija*>(sour))
	{
		QIzvodKorekcija *dest = qobject_cast<QIzvodKorekcija*>(sour);
//		dest->setETM(text6, text8, text9, textName);
		emit eupdateNanigator(dest->getCategoryWidget(), this);
		dest->setFocusLine2();
	}
}

void QMyETM::setInitSearchText(QString text)
{
    if (ETMLista)
    {
        ETMLista->setInitText(text);
    } 
}